package com.luwanglin.eduservice.controller;


import com.luwanglin.commonutils.R;
import com.luwanglin.eduservice.entity.EduChapter;
import com.luwanglin.eduservice.entity.chapter.ChapterVo;
import com.luwanglin.eduservice.service.EduChapterService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    private final EduChapterService eduChapterService;


    public EduChapterController(EduChapterService eduChapterService) {
        this.eduChapterService = eduChapterService;
    }

    @GetMapping("getAllChapterAndVideo/{courseId}")
    public R getChapterAndVideo(@PathVariable("courseId") String courseId) {
        List<ChapterVo> chapterVos = eduChapterService.getChapterAndVideo(courseId);
        return R.ok().data("allChapterAndVideo", chapterVos);
    }

    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        eduChapterService.save(chapter);
        return R.ok();
    }

    @GetMapping("getChapterInfo/{id}")
    public R getChapterInfo(@PathVariable("id") String chapterId) {
        EduChapter chapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }

    @PutMapping("updateChapterInfo")
    public R updateChapterInfo(@RequestBody EduChapter chapter) {
        eduChapterService.updateById(chapter);
        return R.ok();
    }

    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapterInfo(@PathVariable("id") String chapterId) {
        boolean b = eduChapterService.deleteChapter(chapterId);
        return b ? R.ok() : R.error().message("delete failed");
    }
}

